/**
 * **********************常用JS************************
 * preview 预览图片大图
 */

//显示大图
;(function($){
	$.fn.preview = function(){
		var w = $(window).width();
		var h = $(window).height();
		
		$(this).each(function(){
			$(this).hover(function(e){
				if(/.png$|.gif$|.jpg$|.bmp$|.jpeg$/i.test($(this).attr("data-bimg"))){
					$("body").append("<div id='preview'><img src='"+$(this).attr('data-bimg')+"' style='max-width:400px;' /></div>");
				}
				var show_x = $(this).offset().left + $(this).width();
				var show_y = $(this).offset().top;
				var scroll_y = $(window).scrollTop();
				$("#preview").css({
					position:"absolute",
					padding:"4px",
					border:"1px solid #f3f3f3",
					backgroundColor:"#eeeeee",
					top:show_y + "px",
					left:show_x + "px",
					zIndex:1000
				});
				$("#preview > div").css({
					padding:"5px",
					backgroundColor:"white",
					border:"1px solid #cccccc"
				});
				if (show_y + 230 > h + scroll_y) {
					$("#preview").css("bottom", h - show_y - $(this).height() + "px").css("top", "auto");
				} else {
					$("#preview").css("top", show_y + "px").css("bottom", "auto");
				}
				$("#preview").fadeIn("fast")
			},function(){
				$("#preview").remove();
			})					  
		});
	};
})(jQuery);

//打开页面以标签形式展现
function openli(self){
	//左侧菜单点击
		if($('#J_mtab_h li',parent.document).length>=20){
			alert('页面太多了,关掉部分不再使用的页面 !');
			return false;
		}

		$('#J_mtab_h li',parent.document).removeClass('current');
		var data_name=$(self).html(),
			data_uri = $(self).attr('data-uri'),
			data_id = $(self).attr('data-id'),
			_li = $('#J_mtab li[data-id='+data_id+']',parent.document);

		    if($(self).attr('ctitle')){
			   data_name = $(self).attr('ctitle');
		    }else if($(self).attr('title')){
			   data_name = $(self).attr('title');
		    }

		//关闭左侧菜单
		if(1==$(self).attr('offleft')){
            $('#J_lmenu',parent.document).parent().addClass('left_menu_on');
            $('#J_lmoc',parent.document).addClass('close');
            $('html',parent.document).addClass('on');
            $('#J_lmoc',parent.document).data('clicknum', 1);
		}

		//存在则直接点击
		if(_li[0]){
			$('#rframe_'+data_id,parent.document).attr('src',data_uri);
			_li.trigger('click');
		}else{

			//不存在新建frame和tab
			var rframe = $('<iframe/>', {
				src               : data_uri,
				id                  : 'rframe_' + data_id,
				allowtransparency : true,
				frameborder       : 0,
				scrolling          : 'auto',
				width              : '100%',
				height        : '100%'
			}).appendTo($('#J_rframe',parent.document));
			$(rframe[0].contentWindow.document,parent.document).ready(function(){
				rframe.siblings().hide();
				var _li = $('<li data-id="'+data_id+'"><span><a>'+data_name+'</a><a class="del" title="关闭此页">关闭</a></span></li>').addClass('current');
				_li.appendTo($('#J_mtab_h',parent.document)).siblings().removeClass('current');
				_li.trigger('click');
			});
		}
}

//ajax提交表单内容
function ajaxpost(form1){
	ajaxpost_std(form1,1);
}

//提交链接，有提示
function ajaxpostconfirm(form1,cmsg){
	artDialog(
	   {content:cmsg,lock:true,fixed:true,icon:'question',style:'confirm'},
	   function(){ajaxpost_std(form1,1);}, //确认动作
	   function(){} //取消动作
	);
}


//ajax提交表单内容，无刷新
function ajaxnorefrespost(form1){
	ajaxpost_std(form1,2);
}

/**
 * 统一处理表单内容
 * @param form1 表单id名称
 * @param way   表单处理成功动作
 */
function ajaxpost_std(form1,way){
	var obj=$("#"+form1);
	if(obj.data('lock'))return;
	obj.data('lock', true);
	$.post(obj.attr('action'),obj.serialize(),function(result){
		if(!result.code){
			art.dialog({title:'消息',content:result.msg,icon: 'error',fixed:true}).lock().time(2);
			obj.data('lock', false);
			return false;
		}

		if(1==way){
			art.dialog({title:'消息',content:result.msg,icon:'succeed',fixed:true,close:function(){reload_std(result);}}).lock().time(2);
		}else if(2==way){
			art.dialog({title:'消息',content:result.msg,icon:'succeed',fixed:true}).lock().time(2);
			obj.data('lock', false);
		}
	})
}

//提交链接，有提示
function ajaxget(url,cmsg){
	artDialog(
	   {content:cmsg,lock:true,fixed:true,icon:'question',style:'confirm'},
	   function(){ajaxurl(url);}, //确认动作
	   function(){} //取消动作
	);
}

//提交链接，无提示
function ajaxurl(url){
	$.getJSON(url,function(result){
		if(!result.code){
			art.dialog({title:'消息',content:result.msg,icon: 'error',fixed:true}).lock().time(2);
			return false;
		}
		art.dialog({title:'消息',content:result.msg,icon: 'succeed',fixed:true,close:function(){reload_std(result);}}).lock().time(2);
	})
}

/**
 * 统一处理回调后页面动作
 * @param result 返回数据
 */
function reload_std(result){
	if(result.data.cmd==1){
		location.href=result.url;
	}else{
		location.reload(true);
	}
}

/**
 * 提示框
 * @param msg 提示信息
 * @param type 提示方式 1-错误  2-成功
 * @param time 关闭时间
 */
function msgwin(msg,type=1,time=3){
	art.dialog({title:'消息',content:msg,icon: 'error',fixed:true}).lock().time(2);
}
